package com.ziyun.pop.web.modules.storeMaterialCheck.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeMaterialCheck.vo.StoreMaterialCheckDetailVo;
import com.ziyun.pop.modules.storeMaterials.entity.ErpStockEntity;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.pop.modules.storeMaterialCheck.entity.StoreMaterialCheckEntity;
import com.ziyun.pop.modules.storeMaterialCheck.service.StoreMaterialCheckService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 门店耗材盘点表
 *
 * @author xueqz
 * @email 898366526@qq.com
 * @date 2018-12-14 14:47:48
 */
@RestController
@RequestMapping("storeMaterialCheck/storematerialcheck")
public class StoreMaterialCheckController {
    @Autowired
    private StoreMaterialCheckService storeMaterialCheckService;
    @Autowired
    private ErpStockService erpStockService;



    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeMaterialCheck:storematerialcheck:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = storeMaterialCheckService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{checkId}")
    @RequiresPermissions("storeMaterialCheck:storematerialcheck:info")
    public R info(@PathVariable("checkId") Integer checkId){
        StoreMaterialCheckEntity storeMaterialCheck = storeMaterialCheckService.getById(checkId);

        return R.ok().put("storeMaterialCheck", storeMaterialCheck);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeMaterialCheck:storematerialcheck:save")
    public R save(@RequestBody StoreMaterialCheckEntity storeMaterialCheck){
        storeMaterialCheckService.save(storeMaterialCheck);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeMaterialCheck:storematerialcheck:update")
    public R update(@RequestBody StoreMaterialCheckEntity storeMaterialCheck){
        ValidatorUtils.validateEntity(storeMaterialCheck);
        storeMaterialCheckService.updateById(storeMaterialCheck);//全部更新
        
        return R.ok();
    }

//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
//    @RequiresPermissions("storeMaterialCheck:storematerialcheck:delete")
//    public R delete(@RequestBody Integer[] checkIds){
//        storeMaterialCheckService.removeByIds(Arrays.asList(checkIds));
//        return R.ok();
//    }
    /**
     * 获得所有有库存的材料列表
     */
    @RequestMapping("/getStoreStockMaterial")
    public R getStoreStockMaterial(){

        Integer stockId = erpStockService.getStockId(UserUtils.getCurentUser().getSellerId());
        //查询数据将其组装成vo返回
        List<StoreMaterialCheckDetailVo> result = storeMaterialCheckService.handlerToVo(stockId);

        return R.ok().put("storeMaterialCheckDetailVoList",result);
    }

    /**
     * 保存
     */
    @RequestMapping("/checkSave/{updateFlag}")
    public R checkSave(@RequestBody List<StoreMaterialCheckDetailVo> StoreMaterialCheckDetailVoList,@PathVariable("updateFlag") Boolean updateFlag){
        Integer stockId = erpStockService.getStockId(UserUtils.getCurentUser().getSellerId());
        if(updateFlag){
            storeMaterialCheckService.checkUpdate(StoreMaterialCheckDetailVoList,UserUtils.getCurentUser().getUsername());
        }else{
            storeMaterialCheckService.checkSave(StoreMaterialCheckDetailVoList,stockId,UserUtils.getCurentUser());
        }
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/checkInfo/{checkId}")
    @RequiresPermissions("storeMaterialCheck:storematerialcheck:info")
    public R checkInfo(@PathVariable("checkId") Integer checkId){

        return storeMaterialCheckService.checkInfo(checkId);
    }


}
